home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d18
/
futils.arc
/
FSWAP.DOC
< prev
next >
Wrap
Text File
|
1991-04-28
|
2KB
|
80 lines
********************************************************************
************************* FSWAP by Rex Kerr ************************
************************ Copyright (C) 1989 ************************
********************************************************************
This is a tiny unit for Turbo Pascal 5.5, written in assembly
language. Since it is written in assembler, it is both very small
and very fast.
There are just 3 procedures in this unit.
***
QSwapB(var a,b : byte);
This swaps two bytes.
***
QSwapW(var a,b : word);
This swaps two words.
***
QSwapV(var a,b; len : word);
This swaps the first len bytes of a and b.
***
That's it. No more messy procedures like this:
procedure SwapW(var a,b : word);
var temp : word;
begin
temp := a;
a := b;
b := temp
end;
The three QSwaps are better that pascal things like this on two
counts:
1) They are faster (about 50% faster with words).
2) They save stack space, as no temp is needed.
Now, it is true that with large records and arrays ( > 150 bytes)
a pascal procedure that declares an entire one of the record or
array is faster. But is is very stack hungry! Do your own
benchmarks....
type longarray = array[1..1000] of byte;
var abc,xyz : longarry
. . .
procedure swaparray(var firstarray,secondarray : longarray);
var temparray;
begin
temparray := firstarray;
firstarray := secondarray;
secondarray := temparray;
end;
. . .
begin
swaparray(abc,xyz); { This is faster, but it takes }
{ 1K extra in stack space }
qswapv(abc,xyz,sizeof(abc)); { This is almost as fast, and }
{ uses almost no stack space }
end.
The fswap procedures take advantage of an assembly language
command, XCHG. It swaps two bytes or words. It executes about
as fast as a XOR does, so it is about three times faster than
the XORing trick of
a := a xor b;
b := a xor b;
a := a xor b;
Now, why doesn't someone put XCHG into Turbo Pascal?